<template>
    <div>
        <div class="permissManaTool">
            <el-input size="small" placeholder="请输入角色英文名" v-model="role.name">
                <template slot="prepend">ROLE_</template>
            </el-input>
            <el-input size="small" placeholder="请输入角色中文名" v-model="role.nameZh"></el-input>
            <el-button type="primary" size="small" icon="el-icon-plus" @click="addRole">添加角色</el-button>
        </div>
        <div class="permissManaMain">
            <el-collapse v-model="activeName" accordion @change="change">
                <el-collapse-item :title="r.nameZh" :name="r.id" v-for="(r, index) in roles" :key="index">
                    <el-card class="box-card">
                        <div slot="header" class="clearfix">
                            <span>可访问的资源</span>
                            <el-button style="float: right; padding: 3px 0; color: #ff0000;" icon="el-icon-delete" type="text" @click="removeRole(r)"></el-button>
                        </div>
                        <div>
                            <el-tree show-checkbox node-key="id" ref="tree" :default-checked-keys="selectedMenus" :data="allmenus" :props="defaultProps"></el-tree>
                            <div style="display: flex; justify-content: flex-end; margin-top: 2px">
                                <el-button size="mini" @click="cancelUpdate">取消修改</el-button>
                                <el-button size="mini" type="primary" @click="doUpdate(r.id, index)">确认修改</el-button>
                            </div>
                        </div>
                    </el-card>
                </el-collapse-item>
            </el-collapse>
        </div>
    </div>
</template>

<script>
    export default {
        name: "PermissMana",
        data(){
            return{
                activeName: -1,
                role: {
                    name: '',
                    nameZh: ''
                },
                roles: [],
                allmenus: [],
                selectedMenus: [],
                defaultProps: {
                    children: 'children',
                    label: 'name'
                }
            }
        },
        mounted(){
          this.initRoles();
        },
        methods: {
            // 删除角色
            removeRole(role){
                this.$confirm('此操作将永久删除【' + role.nameZh + '】, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest("/system/basic/permiss/role/" + role.id).then(resp => {
                        if (resp) {
                            this.initRoles();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });            },
            // 添加角色
            addRole(){
                if (this.role.name && this.role.nameZh){
                    this.postRequest('/system/basic/permiss/role', this.role).then(resp => {
                        if (resp){
                            this.name = '';
                            this.nameZh = '';
                            this.initRoles();
                        }
                    });
                } else {
                    this.$message.error("数据不可为空!");
                }
            },
            // 取消修改权限
            cancelUpdate(){
                this.activeName = -1;
            },
            // 确认修改权限
            doUpdate(rid, index){
                console.log(rid);
                let tree = this.$refs.tree[index];
                let checkedKeys = tree.getCheckedKeys(true);
                console.log(checkedKeys);
                let url = '/system/basic/permiss/?rid=' + rid;
                checkedKeys.forEach(key => {
                    url += '&mids=' + key;
                });
                this.putRequest(url).then(resp => {
                    if (resp){
                        this.activeName = -1;
                    }
                })
            },
            // 更新
            change(rid) {
                if (rid){
                    this.initAllMenus();
                    this.initSelectdMenus(rid);
                }
            },
            // 获取选中的菜单
            initSelectdMenus(rid){
                this.getRequest("/system/basic/permiss/mids/" + rid).then(resp => {
                    if (resp){
                        this.selectedMenus = resp;
                    }
                })
            },
            // 所有菜单初始化刷新
            initAllMenus(){
                this.getRequest("/system/basic/permiss/menus").then(resp => {
                    if (resp){
                        this.allmenus = resp;
                    }
                })
            },
            // 所有角色初始化刷新
            initRoles(){
                this.getRequest("/system/basic/permiss/").then(resp => {
                    if (resp){
                        this.roles = resp;
                    }
                })
            }
        }
    }
</script>

<style>
    .permissManaTool {
        display: flex;
        justify-content: flex-start;
    }

    .permissManaTool .el-input{
        width: 250px;
        margin-right: 6px;
    }

    .permissManaMain {
        margin-top: 10px;
        width: 600px;
    }

</style>
